package com.mall.order.biz.handler;

import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.shopping.ICartService;
import com.mall.shopping.dto.CartProductDto;
import com.mall.shopping.dto.DeleteCheckedItemRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.List;

//import com.mall.order.dto.CartProductDto;

/**
 *  ciggar
 * create-date: 2019/8/1-下午5:05
 * 将购物车中的缓存失效
 */
@Slf4j
@Component
public class ClearCartItemHandler extends AbstractTransHandler {


    @Reference(check = false)
    ICartService iCartService;
    //是否采用异步方式执行
    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {

        //根据订单信息清空购物车，即购物车选中商品

        CreateOrderContext orderContext = (CreateOrderContext) context;
         Long userId = orderContext.getUserId();
        List<CartProductDto> cartProductDtoList = orderContext.getCartProductDtoList();

        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(userId);

        //获取商品id,封装并删除购物车
        for (CartProductDto cartProductDto : cartProductDtoList) {
            request.setPid(cartProductDto.getProductId());
            iCartService.deleteCheckedItem(request);
        }

        return true;
    }
}
